//
//  MyTableViewCell.m
//  Gxj
//
//  Created by 💋索少杰 on 2017/9/1.
//  Copyright © 2017年 wkq. All rights reserved.
//

#import "MyTableViewCell.h"
#import <SDAutoLayout.h>
#import <SDWebImage/UIImageView+WebCache.h>
#import "GXJPrefix.pch"

#define Start_X          75.0f      // 第一个按钮的X坐标
#define Start_Y          260.0f     // 第一个按钮的Y坐标
#define Width_Space      -8.0f      // 2个按钮之间的横间距
#define Height_Space     20.0f     // 竖间距
#define Button_Height   23.0f    // 高
#define Button_Width    23.0f    // 宽



@implementation MyTableViewCell

// 重写表格位置，使每一行cell都分隔开
//- (void)setFrame:(CGRect)frame{
//    //    frame.origin.x += 10;
//    frame.origin.y += 5;
//    frame.size.height -= 5;
//    //    frame.size.width -= 20;
//    [super setFrame:frame];
//}
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        
        
        //用户点击头像响应
        //头像背景
        [self.contentView addSubview:self.viewIcon];
        [self.viewIcon addSubview:self.iconTouxiang];
        //用户名字显示
        [self.contentView addSubview:self.nameText];
        //用户等级图标
        [self.contentView addSubview:self.dengjiTB];
        //用户等级显示
        [self.contentView addSubview:self.dengjiText];
        //浏览人数图标
        [self.contentView addSubview:self.iconLiulang];
        //观看人数
        [self.contentView addSubview:self.liulangNum];
        //用户发表内容
        [self.contentView addSubview:self.contentText];
        //发表时间图标
        [self.contentView addSubview:self.iconShijian];
        //用户发表时间
        [self.contentView addSubview:self.shijianText];
        //点赞数量
        [self.contentView addSubview:self.dianzanNum];
        //点赞图片
        [self.contentView addSubview:self.dianzanIcon];
        //简介阴影
        [self.contentView addSubview:self.jianJieView];
        //简介内容
        [self.jianJieView addSubview:self.jianjieContext];
        //用户发表图片
        [self.contentView addSubview:self.fabiaoImage];
        //关注按钮
        [self.contentView addSubview:self.guanzhu];
        //最佳讨论文字
        [self.contentView addSubview:self.zuijiaText];
        //最佳用户头像
//        [self addSubview:self.iconZuijia];
        //添加最佳用户头像
        [self.iconZuijia addSubview:self.addBut];
        //Cell底部线条
        [self.contentView addSubview:self.lineBack];
        //商业标签
        [self.fabiaoImage addSubview:self.theLabel];
        [self.fabiaoImage addSubview:self.theLabel2];
        [self.fabiaoImage addSubview:self.theLabel3];
        //适配方法
        [self setUpadapter];
        
        
        //商业标签
//        for (int i = 0; i < 3; i++) {
//            _theLabel = [[UILabel alloc] initWithFrame:CGRectMake(kWidthScale(5) + kWidthScale(40) * i, kHeightScale(5), kWidthScale(35), kHeightScale(15))];
//            
//            _theLabel.backgroundColor = [UIColor colorWithWhite:0 alpha:0.4];
//            _theLabel.text = @"商业";
//            _theLabel.textColor = [UIColor colorWithWhite:1 alpha:1];
//            _theLabel.font = [UIFont systemFontOfSize:kWidthScale(9)];
//            _theLabel.textAlignment = NSTextAlignmentCenter;
//            _theLabel.layer.cornerRadius = kWidthScale(5);
//            _theLabel.layer.masksToBounds = YES;
//            
//            [self.fabiaoImage addSubview:self.theLabel];
//        }
        
        //最佳头像
        NSArray*arr=@[@"toixiang",@"toixiang",@"toixiang",];
        for (int i = 0 ; i < arr.count; i++) {
            NSInteger index = i % arr.count;
            NSInteger page = i / arr.count;
            
            // 圆角按钮
            _iconZuijia = [[UIImageView alloc] init];
            //_iconZuijia.tag = i;//这句话不写等于废了
            _iconZuijia.frame = CGRectMake(index * (Button_Width + Width_Space) + Start_X, page  * (Button_Height + Width_Space )+Start_Y, Button_Width, Button_Height);
            _iconZuijia.image=[UIImage imageNamed:arr[index]];
            [self addSubview:self.iconZuijia];
        }
        
//        //最佳用户头像
//        for (int i = 0; i < 5; i++) {
//            _iconZuijia = [[UIImageView alloc] initWithFrame:CGRectMake(kWidthScale(84) * (i+1) - (66*i), kHeightScale(260), kWidthScale(20), kHeightScale(20))];
//            
//            _iconZuijia.image=[UIImage imageNamed:@"toixiang"];
//            [self.contentView addSubview:_iconZuijia];
//            
//        }


    }
    return self;
}


//适配方法
- (void)setUpadapter
{
    //头像背景
    _viewIcon.sd_layout
    .topSpaceToView(self.nameText, kHeightScale(-12.5))
    .leftSpaceToView(self.contentView, kWidthScale(15))
    .widthIs(kWidthScale(25))
    .heightIs(kHeightScale(25));
    
    //头像
    _iconTouxiang.sd_layout
    .topSpaceToView(self.viewIcon, kHeightScale(2))
    .leftSpaceToView(self.viewIcon, kWidthScale(2))
    .rightSpaceToView(self.viewIcon,kWidthScale(2))
    .bottomSpaceToView(self.viewIcon,kHeightScale(2));
    
    //名字
    _nameText.sd_layout
    .topSpaceToView(self.contentView, kHeightScale(10))
    .leftSpaceToView(self.viewIcon, kWidthScale(5))
    .widthIs(kWidthScale(100))
    .autoHeightRatio(0);
    
    //等级图标
    _dengjiTB.sd_layout
    .topSpaceToView(self.nameText, kHeightScale(3))
    .leftSpaceToView(self.viewIcon, kWidthScale(5))
    .widthIs(kWidthScale(11))
    .heightIs(kHeightScale(11));
    //等级时间
    _dengjiText.sd_layout
    .centerYEqualToView(self.dengjiTB)
    .leftSpaceToView(self.dengjiTB, kWidthScale(3))
    .widthIs(kWidthScale(30))
    .autoHeightRatio(0);
    //发表内容
    _contentText.sd_layout
    .topSpaceToView(self.viewIcon, kHeightScale(10))
    .leftSpaceToView(self.contentView, kWidthScale(15))
    .rightSpaceToView(self.contentView, kWidthScale(15))
    .autoHeightRatio(0);

    //时间图标
    _iconShijian.sd_layout
    .topSpaceToView(self.contentText,kHeightScale(5))
    .leftEqualToView(self.viewIcon)
    .widthIs(kWidthScale(13))
    .heightIs(kHeightScale(13));
    //时间
    _shijianText.sd_layout
    .topSpaceToView(self.contentText, kHeightScale(4))
    .leftSpaceToView(self.iconShijian, kWidthScale(5))
    .autoHeightRatio(0);
    [_shijianText setSingleLineAutoResizeWithMaxWidth:(40)];
    //浏览图标
    _iconLiulang.sd_layout
    .centerYEqualToView(self.iconShijian)
    .leftSpaceToView(self.shijianText, kWidthScale(10))
    .widthIs(kWidthScale(13))
    .heightIs(kHeightScale(8.6));
    //浏览人数
    _liulangNum.sd_layout
    .topEqualToView(self.shijianText)
    .leftSpaceToView(self.iconLiulang, kWidthScale(5))
    .autoHeightRatio(0);
    [_liulangNum setSingleLineAutoResizeWithMaxWidth:(40)];
    //点赞图片
    _dianzanIcon.sd_layout
    .topSpaceToView(self.contentText, kHeightScale(5))
    .leftSpaceToView(self.liulangNum, kWidthScale(10))
    .widthIs(kWidthScale(12))
    .heightIs(kHeightScale(12));
    //点赞数量
    _dianzanNum.sd_layout
    .topEqualToView(self.shijianText)
    .leftSpaceToView(self.dianzanIcon, kWidthScale(5))
    .autoHeightRatio(0);
    [_dianzanNum setSingleLineAutoResizeWithMaxWidth:(40)];
    //简介View阴影
    _jianJieView.sd_layout
    .topSpaceToView(self.shijianText, kHeightScale(5))
    .leftSpaceToView(self.contentView, kWidthScale(15))
    .rightSpaceToView(self.contentView, kWidthScale(15))
    .heightIs(kHeightScale(49));
    //简介
    _jianjieContext.sd_layout
    .topSpaceToView(self.jianJieView, 0)
    .leftSpaceToView(self.jianJieView, 0)
    .rightSpaceToView(self.jianJieView, 0)
    .heightIs(kHeightScale(49));
    //发表图片
    _fabiaoImage.sd_layout
    .topSpaceToView(self.jianJieView, kHeightScale(10))
    .leftSpaceToView(self.contentView, kWidthScale(15))
    .rightSpaceToView(self.contentView, kWidthScale(15))
    .heightIs(kHeightScale(110));
    //商业标签
    _theLabel.sd_layout
    .topSpaceToView(self.fabiaoImage,kHeightScale(5))
    .leftSpaceToView(self.fabiaoImage,kWidthScale(5))
    .widthIs(kWidthScale(35))
    .heightIs(kHeightScale(15));
    _theLabel2.sd_layout
    .topSpaceToView(self.fabiaoImage,kHeightScale(5))
    .leftSpaceToView(self.theLabel,kWidthScale(5))
    .widthIs(kWidthScale(35))
    .heightIs(kHeightScale(15));
    _theLabel3.sd_layout
    .topSpaceToView(self.fabiaoImage,kHeightScale(5))
    .leftSpaceToView(self.theLabel2,kWidthScale(5))
    .widthIs(kWidthScale(35))
    .heightIs(kHeightScale(15));
    //最佳文字
    _zuijiaText.sd_layout
    .topSpaceToView(self.fabiaoImage, kHeightScale(10))
    .leftSpaceToView(self.contentView, kWidthScale(15))
    .widthIs(kWidthScale(50))
    .autoHeightRatio(0);
    //最佳添加按钮
    _addBut.sd_layout
    .topSpaceToView(self.iconZuijia,kHeightScale(0))
    .leftSpaceToView(self.iconZuijia, kWidthScale(-5))
    .widthIs(kWidthScale(20))
    .heightIs(kHeightScale(20));
    //关注
    _guanzhu.sd_layout
    .topSpaceToView(self.nameText,kHeightScale(-12.5))
    .rightSpaceToView(self.contentView,kWidthScale(15))
    .widthIs(kWidthScale(44))
    .heightIs(kHeightScale(25));
    //cell底部的线条
    _lineBack.sd_layout
    .bottomSpaceToView(self.contentView, 0)
    .leftSpaceToView(self.contentView, 0)
    .rightSpaceToView(self.contentView, 0)
    .heightIs(kHeightScale(5));
    
}
//头像背景
- (UIImageView *)viewIcon
{
    if (!_viewIcon)
    {
        _viewIcon=[[UIImageView alloc] init];
        _viewIcon.backgroundColor = [UIColor colorWithRed:(213.0/255) green:(213.0/255) blue:(213.0/255) alpha:1];;
        _viewIcon.layer.masksToBounds=YES;
        _viewIcon.layer.cornerRadius = kHeightScale(12.5);
        _viewIcon.layer.shadowColor = [UIColor blackColor].CGColor;
        _viewIcon.layer.shadowOffset = CGSizeMake(-3, 1);
        _viewIcon.layer.shadowOpacity = 0.4;
        _viewIcon.layer.shadowRadius = 2.0;
        _viewIcon.clipsToBounds = NO;
        
        _viewIcon.image = [UIImage imageNamed:@"touxiang"];
        _viewIcon.contentMode = UIViewContentModeScaleAspectFit;
    }
    return _viewIcon;
}
//头像
- (UIButton *)iconTouxiang
{
    if (!_iconTouxiang)
    {
        _iconTouxiang=[[UIButton alloc] init];
        [_iconTouxiang setBackgroundImage:[UIImage imageNamed:@"toixiang"] forState:UIControlStateNormal];
        _iconTouxiang.layer.masksToBounds=YES;
        _iconTouxiang.layer.cornerRadius=kWidthScale(23/2);
    }
    return _iconTouxiang;
}
//名字
- (UILabel *)nameText
{
    if (!_nameText)
    {
        _nameText=[[UILabel alloc] init];
        _nameText.text=@"后悔的小公主";
        _nameText.font = [UIFont systemFontOfSize:kHeightScale(12)];
        _nameText.textColor = [UIColor colorWithHexString:@"#333333"];
    }
    return _nameText;
}
//等级图标
- (UIImageView *)dengjiTB
{
    if (!_dengjiTB)
    {
        _dengjiTB=[[UIImageView alloc] init];
        _dengjiTB.image=[UIImage imageNamed:@"dengjis"];
    }
    return _dengjiTB;
}
//等级
- (UILabel *)dengjiText
{
    if (!_dengjiText)
    {
        _dengjiText=[[UILabel alloc] init];
        _dengjiText.text=@"100级";
        _dengjiText.textColor=[UIColor colorWithRed:(153.0/255) green:(153.0/255) blue:(153.0/255) alpha:1];
        _dengjiText.font=[UIFont systemFontOfSize:kWidthScale(10)];
    }
    return _dengjiText;
}
//用户发表动态
- (UILabel *)contentText
{
    if (!_contentText)
    {
        _contentText=[[UILabel alloc] init];
        _contentText.text=@"梦想有多大就能飞多远";
        _contentText.textColor = [UIColor colorWithHexString:@"#333333"];
        _contentText.font=[UIFont systemFontOfSize:kWidthScale(14)];
    }
    return _contentText;
}
//发表时间图标
- (UIImageView *)iconShijian
{
    if (!_iconShijian)
    {
        _iconShijian=[[UIImageView alloc] init];
        _iconShijian.image=[UIImage imageNamed:@"time"];
    }
    return _iconShijian;
}
//发表时间
- (UILabel *)shijianText
{
    if (!_shijianText)
    {
        _shijianText=[[UILabel alloc] init];
        _shijianText.text=@"15:20";
        _shijianText.textColor=[UIColor colorWithRed:(153.0/255) green:(153.0/255) blue:(153.0/255) alpha:1];
        _shijianText.font=[UIFont systemFontOfSize:kWidthScale(12)];
    }
    return _shijianText;
}
//浏览图标
- (UIImageView *)iconLiulang
{
    if (!_iconLiulang)
    {
        _iconLiulang=[[UIImageView alloc] init];
        _iconLiulang.image=[UIImage imageNamed:@"liulans"];
    }
    return _iconLiulang;
}
//浏览人数
- (UILabel *)liulangNum
{
    if (!_liulangNum)
    {
        _liulangNum=[[UILabel alloc] init];
        _liulangNum.text=@"8461";
        _liulangNum.textColor=[UIColor colorWithRed:(153.0/255) green:(153.0/255) blue:(153.0/255) alpha:1];
        _liulangNum.font=[UIFont systemFontOfSize:kWidthScale(12)];
    }
    return _liulangNum;
}
//点赞图片
- (UIImageView *)dianzanIcon
{
    if (!_dianzanIcon)
    {
        _dianzanIcon=[[UIImageView alloc] init];
        _dianzanIcon.image=[UIImage imageNamed:@"zans"];
    }
    return _dianzanIcon;
}
//点赞数量
- (UILabel *)dianzanNum
{
    if (!_dianzanNum)
    {
        _dianzanNum=[[UILabel alloc] init];
        _dianzanNum.text=@"1000";
        _dianzanNum.textColor=[UIColor colorWithRed:(153.0/255) green:(153.0/255) blue:(153.0/255) alpha:1];
        _dianzanNum.font=[UIFont systemFontOfSize:kWidthScale(12)];
    }
    return _dianzanNum;
}
//背景颜色
- (UIView *)jianJieView
{
    if (!_jianJieView)
    {
        _jianJieView = [[UIView alloc] init];
//        _jianJieView.layer.shadowColor = [UIColor blackColor].CGColor;
//        _jianJieView.layer.shadowOffset = CGSizeMake(0, 1);
//        _jianJieView.layer.shadowOpacity = 0.2;
//        //        _shadowView.layer.shadowRadius = 6.0;
//        //        _shadowView.layer.cornerRadius = 6.0;
//        _jianJieView.clipsToBounds = NO;
        _jianJieView.backgroundColor=[UIColor colorWithPatternImage:[UIImage imageNamed:@"jianjie-bg"]];
        _jianJieView.contentMode = UIViewContentModeScaleAspectFit;
    }
    return _jianJieView;
}
//简介内容
- (KQlable *)jianjieContext
{
    if (!_jianjieContext)
    {
        _jianjieContext = [[KQlable alloc] init];
        _jianjieContext.text = @"风暴简介：个环境可凉快和包教包零零落落零零落落零零落落零零落落零零落落啦啦啦\n";
        _jianjieContext.backgroundColor = [UIColor clearColor];//[UIColor colorWithRed:251.0/255 green:251.0/255 blue:251.0/255 alpha:1]
        _jianjieContext.textColor = [UIColor colorWithHexString:@"#666666"];
        _jianjieContext.font=[UIFont systemFontOfSize:kWidthScale(11)];
        _jianjieContext.layer.cornerRadius = kWidthScale(10);
        _jianjieContext.layer.masksToBounds = YES;
        _jianjieContext.numberOfLines=0;
//        NSDictionary *attrDict1 = @{ NSForegroundColorAttributeName: [UIColor colorWithHexString:@"#666666"] };
//        NSString *str = @"风暴简介：";
//        _jianjieContext.attributedText = [[NSAttributedString alloc] initWithString: str attributes: attrDict1];

        [KQlable changeLineSpaceForLabel:_jianjieContext WithSpace:5.0f];
    }
    return _jianjieContext;
}
- (void)drawTextInRect:(CGRect)rect {
    UIEdgeInsets insets = {0, 5, 0, 5};
    [self drawTextInRect:UIEdgeInsetsInsetRect(rect, insets)];
}

//发表图片
- (UIImageView *)fabiaoImage
{
    if (!_fabiaoImage)
    {
        _fabiaoImage=[[UIImageView alloc] init];
        _fabiaoImage.image=[UIImage imageNamed:@"banner"];
        _fabiaoImage.layer.masksToBounds=YES;
        _fabiaoImage.layer.cornerRadius=kWidthScale(12);
    }
    return _fabiaoImage;
}
//商业标签
- (UILabel *)theLabel
{
    if (!_theLabel)
    {
        _theLabel = [[UILabel alloc] init];
        _theLabel.backgroundColor = [UIColor colorWithWhite:0 alpha:0.3];
        _theLabel.textColor = [UIColor colorWithWhite:1 alpha:1];
        _theLabel.font = [UIFont systemFontOfSize:kWidthScale(9)];
        _theLabel.textAlignment = NSTextAlignmentCenter;
        _theLabel.layer.cornerRadius = kWidthScale(5);
        _theLabel.layer.masksToBounds = YES;
    }
    return _theLabel;
}
- (UILabel *)theLabel2
{
    if (!_theLabel2)
    {
        _theLabel2 = [[UILabel alloc] init];
        _theLabel2.backgroundColor = [UIColor colorWithWhite:0 alpha:0.3];
        _theLabel2.textColor = [UIColor colorWithWhite:1 alpha:1];
        _theLabel2.font = [UIFont systemFontOfSize:kWidthScale(9)];
        _theLabel2.textAlignment = NSTextAlignmentCenter;
        _theLabel2.layer.cornerRadius = kWidthScale(5);
        _theLabel2.layer.masksToBounds = YES;
    }
    return _theLabel2;
}
- (UILabel *)theLabel3
{
    if (!_theLabel3)
    {
        _theLabel3 = [[UILabel alloc] init];
        _theLabel3.backgroundColor = [UIColor colorWithWhite:0 alpha:0.3];
        _theLabel3.textColor = [UIColor colorWithWhite:1 alpha:1];
        _theLabel3.font = [UIFont systemFontOfSize:kWidthScale(9)];
        _theLabel3.textAlignment = NSTextAlignmentCenter;
        _theLabel3.layer.cornerRadius = kWidthScale(5);
        _theLabel3.layer.masksToBounds = YES;
    }
    return _theLabel3;
}
//最佳讨论
- (UILabel *)zuijiaText
{
    if (!_zuijiaText)
    {
        _zuijiaText=[[UILabel alloc] init];
        _zuijiaText.text=@"最佳讨论";
        _zuijiaText.textColor=[UIColor colorWithHexString:@"#666666"];
        _zuijiaText.font=[UIFont systemFontOfSize:kWidthScale(11)];
    }
    return _zuijiaText;
}

//添加最佳讨论人头像
- (UIButton *)addBut
{
    if (!_addBut)
    {
        _addBut=[[UIButton alloc] init];
        [_addBut setBackgroundImage:[UIImage imageNamed:@"jias"] forState:UIControlStateNormal];
    }
    return _addBut;
}
//关注
- (UIButton *)guanzhu
{
    if (!_guanzhu)
    {
        _guanzhu = [UIButton buttonWithType:UIButtonTypeCustom];
        [_guanzhu setBackgroundImage:[UIImage imageNamed:@"guanzhu"] forState:0];
        _guanzhu.contentMode = UIViewContentModeScaleAspectFit;
    }
    return _guanzhu;
}
//cell底部的线条
- (UILabel *)lineBack
{
    if (!_lineBack)
    {
        _lineBack=[[UILabel alloc] init];
        _lineBack.backgroundColor=[UIColor colorWithRed:242.0/255.0 green:242.0/255.0 blue:242.0/255.0 alpha:1];
//        _lineBack.backgroundColor=[UIColor orangeColor];
    }
    return _lineBack;
}

// 数据转接
- (void)setModel:(ImaginationThestormModel_Two *)model
{
    [_iconTouxiang sd_setBackgroundImageWithURL:[NSURL URLWithString:model.head_img] forState:0 placeholderImage:[UIImage imageNamed:@""]];
    _nameText.text=model.nickname;
    _dengjiText.text = [NSString stringWithFormat:@"%@级",[model.level stringValue]];
    _contentText.text=model.info;
    _liulangNum.text=[NSString stringWithFormat:@"%@", [model.viewnum stringValue]];
    BOOL result = [self StringIsNullOrEmpty:[model.viewnum stringValue]];
    
    if (result) {
        _liulangNum.text = @"0";
    }else{
        _liulangNum.text = [model.viewnum stringValue];
    }
    NSString *timeCurrent = [self timeWithTimeIntervalString:[model.create_date stringValue]];
    _shijianText.text = timeCurrent;
    
    //_jianjieContext.text = [NSString stringWithFormat:@"风暴简介:%@",model.info];
    
    _contentText.text = model.title;
    self.theLabel .hidden = NO;
    self.theLabel2.hidden = NO;
    self.theLabel3.hidden = NO;
    if (model.industryid.count == 3) {
        self.theLabel.text = model.industryid[0];
        self.theLabel2.text = model.industryid[1];
        self.theLabel3.text = model.industryid[2];
    }else if (model.industryid.count == 2){
        self.theLabel.text = model.industryid[0];
        self.theLabel2.text = model.industryid[1];
        self.theLabel3.hidden = YES;
    }else{
        self.theLabel.text = model.industryid[0];
        self.theLabel2.hidden = YES;
        self.theLabel3.hidden = YES;
    }
    
    [_shijianText updateLayout];
    [_liulangNum updateLayout];
    [_dianzanNum updateLayout];
}
// 若后台返回值为NULL或者<NULL>时，返回yes
- (BOOL)StringIsNullOrEmpty:(NSString *)str
{
    return (str == nil || [str isKindOfClass:[NSNull class]] || str.length == 0);
}
// 时间戳转换
- (NSString *)timeWithTimeIntervalString:(NSString *)timeString
{
    NSTimeInterval interval    =[timeString doubleValue] / 1000.0;
    NSDate *date               = [NSDate dateWithTimeIntervalSince1970:interval];
    
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"HH:mm"]; // yyyy-MM-dd HH:mm:ss
    NSString *dateString       = [formatter stringFromDate: date];
    
    return dateString;
}




@end
